$(document).ready(function() {
  _backtotop();
});

function _backtotop() {
  // init
  const $window = $(window);
  const $body = $('body');
  const $htmlBody=$('html, body');
  // button
  const $button = $('<a class="backtotop"><i class="fas fa-angle-up"></i></a>');
  $button.click(()=>{
    $htmlBody.animate({scrollTop:0},<%=site.plugins['cms-pluginbacktotop'].animateSpeed%>);
  });
  $body.append($button);

  // onScroll
  const _onScroll = function() {
    const visible=$window.scrollTop()>$(window).height()/2;
    if(visible){
      $button.fadeIn();
    }else{
      $button.fadeOut();
    }
  };

  // bind event
  $window.on('scroll.infinite resize.infinite', _onScroll);
  _onScroll();
};

